//
//  WDMaterialCell.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/27.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDMaterialCell.h"
#import "WDPDBottom.h"
#import "WDProductDetail.h"
#import "WDPDLeftBottom.h"
#import "WDCollectionCell.h"
#import "WDMaterialList.h"

static NSString *cellID = @"collectionCell";

@interface WDMaterialCell () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
/** collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;
/** collectionArr */
@property (nonatomic, strong) NSArray *dataArr;
@property(nonatomic,assign)CGFloat hightED;

@end

@implementation WDMaterialCell

//懒加载collectionView
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        //滚动方向（竖直方向）
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        
        _collectionView.delegate = self;
        
        _collectionView.dataSource = self;
        
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([WDCollectionCell class]) bundle:nil];
        
        [_collectionView registerNib:nib forCellWithReuseIdentifier:cellID];
        
        _collectionView.backgroundColor = [UIColor clearColor];
        
    }
    
    return _collectionView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    //图片自适应
    self.iconView.contentMode = UIViewContentModeScaleAspectFit;
    
    //添加资料控件
    [self.contentView addSubview:self.collectionView];
    
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftLabel.bottom).offset(16);
        make.left.equalTo(30);
        make.right.equalTo(-20);
        make.height.equalTo(120);//先随定一个
    }];
    
}

+ (instancetype)materialCellWithTableView:(UITableView *)tablView
{
    static NSString *ID = @"materialCell";
    
    WDMaterialCell *cell = [tablView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    return cell;
}

#pragma mark - 模型赋值
- (void)setBottom:(WDPDBottom *)bottom
{
    _bottom = bottom;
    
    bottom.cellHeight = self.productDetail.collectionH;
}

- (void)setLeftBottom:(WDPDLeftBottom *)leftBottom
{
    _leftBottom = leftBottom;
    
    self.iconView.image = [UIImage imageNamed:leftBottom.icon];
    
    self.leftLabel.text = leftBottom.intro;
}

- (void)setProductDetail:(WDProductDetail *)productDetail
{
    //刷新collectionView
    [self.collectionView reloadData];
    
    _productDetail = productDetail;
    
    self.dataArr = [WDMaterialList mj_objectArrayWithKeyValuesArray:productDetail.material_list];
    
    [self layoutIfNeeded];
    
    productDetail.collectionH = CGRectGetMaxY(self.collectionView.frame);
}

#pragma mark - collectionView代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArr.count == 0) {
        
        return 1;
        
    } else {
        
        return self.dataArr.count;
        
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WDCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    cell.material = self.dataArr[indexPath.row];
    
    [self updateCollectionViewHight:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(132, 151);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *images = [NSMutableArray array];
    WDMaterialList *material;
    NSString *imageURL;
    for (material in self.dataArr) {
        imageURL = material.big_image_url;
        [images addObject:imageURL];
    }
    
    [_delegate materialCell:self ShowOriginalImages:images index:indexPath.row];
    
    //WDLog(@"点击了第%zd个图片", indexPath.row);
}

-(void)updateCollectionViewHight:(CGFloat)hight
{
    
    if (self.hightED != hight) { //这个判断起到两个作用 第一 以为这个方法被调用多次这样写 保证 每个cell里面调用一次,切只调用一次  第二是当cell被重用从用的cell上的collectionView内容高度不一样的时候重新 更新跟新高度
        self.hightED = hight;
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(hight);
        }];
    }
}

@end
